<?php

/**
 * 
 */
class Addcomment extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('comments_model');
        $this->load->model('reports_model');
    }

    public function insert() { // $title,$text,$report_id)
        $title = $this->input->post('title');
        $text = $this->input->post('text');
        $report_id = $this->input->post('id');
        $id_ = $this->session->userdata("sess_user_id");
        $date = date('Y-m-d H:i:s');
        if (!empty($title) && !empty($text) && !empty($report_id) && ($id_ != null)) {
            $this->comments_model->insertComment($id_, $title, $text, $report_id, $date);
            $this->reports_model->plusOneComment($report_id);
            echo 'ok';
        }
    }

    public function getLastCommentByUser($report_id) {

        if ($this->comments_model->fetchComments($report_id) != null) {
            $user_id = $this->session->userdata("sess_user_id");
            $data['comment'] = $this->comments_model->fetchLastCommentByUser($user_id, $report_id);
            $data['comment_title'] = ucfirst($data['comment'][0]['title']); // 
            $data['comment_author'] = $data['comment'][0]['login'];
            $data['comment_id'] = $data['comment'][0]['comment_id']; //
            $data['comment_text'] = $data['comment'][0]['comment_text'];
            $data['comment_date'] = $this->dateFormatToSlovak($data['comment'][0]['added']);
            $this->load->view('templates/newComment', $data);
        }
    }

    public function getComments($report_id) {

        if ($this->comments_model->fetchComments($report_id) != null) {
            $data['comment'] = $this->comments_model->fetchComments($report_id);
            foreach ($data['comment'] as $key) {
                $plus = $this->comm++;
                $data['comment_title'][$plus] = $key['title']; // 
                $data['comment_id'][$plus] = $key['comment_id']; // 
                $data['comment_author'][$plus] = $key['login'];
                $data['comment_text'][$plus] = $key['comment_text'];
                $data['comment_date'][$plus] = $this->dateFormatToSlovak($key['added']);
            }
            $this->load->view('templates/comments', $data);
        }
    }

    public function report($comment_id) {
        $user_id = $this->session->userdata("sess_user_id");
        $this->comments_model->reportComment($user_id, $comment_id);
    }

    public function dateFormatToSlovak($date) {
        return date('d.m.Y H:i:s', strtotime($date));
    }

}
